var app;
jQuery(document).ready(function() {
	app = new Vue({
        el: '#app',
        data: {
        	tableSize : 'default',
        	fixedHeader : 560,
        	showTabs : false,
        	showFieldJSON : false,
        	fieldJSONText : '', 
        	showScript : false,
        	scriptTabName : '1',
        	scriptText : '',
        	scriptList : [ ],
        	scriptUse : [ {
        		src:'/mobile/plugin/westvalley/common/js/common.js'
        	},{
        		src:'/mobile/plugin/westvalley/common/js/workFlowField.js'
        	}],
        	actionAllList : [ ],
        	actionList : [ ],
        	workflowId : '',
            workflowName : '',
            filterVa1 : '',
            workflowDataTemp : ['无数据'],
            workflowData : [ ],
            workflowNodeId : '',
            workflowNodeLabel : '',
            workflowNodeData : [ ],
            workflowFieldData :[ ],
            workflowFieldColums :[ {
            	type: 'index',
            	width: 60,
            	align: 'center'
            },{
            	title:'字段id',
            	key:'fieldId'
            },{
            	title:'字段名',
            	key:'fieldName'
            },{
            	title:'字段类型',
            	key:'fieldType'
            },{
            	title:'显示名',
            	key:'labelName'
            } ]
        },
        methods: {
        	filterMethod : function (value, option){
        		this.filterVa1 = value;
        		loadWorkFlowInfo(this);
        		return 1;
        	},
        	selectOption : function(v){
        		var ref = this.$refs["li_"+v];
        		if(ref){
        			var els = jQuery(ref[0].$el).find("span");
        			if( els.length == 3 ){
        				this.workflowId = els[0].innerText;
        				this.workflowName = els[1].innerText;
        				this.filterVa1 = els[0].innerText;
        				v = els[1].innerText;
        			}
        		}else{
        			this.workflowId = "";
    				this.workflowName = "";
    				this.filterVa1 = "";
        			v = "";
        		}
        		loadWorkFlowFieldInfo();
        		return v;
        	},
        	showFieldSwitch : function(){
        		if(this.workflowId!=''){
        			this.showFieldJSON = true;
        		}else{
        			this.showFieldJSON = false;
        			this.$Message.warning('请选择流程!');
        		}
        	},
        	showScriptSwitch : function(){
        		this.showScript = false;
        		if(this.workflowId==''){
        			this.$Message.warning('请选择流程!');
        		}else if(this.workflowNodeId==''){
        			this.$Message.warning('请选择节点!');
        		}else{
        			this.showScript = true;
        			var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFNodeScriptInfo.jsp";
        			var data = {
        				workFlowId : this.workflowId,
        				nodeId : this.workflowNodeId
        			};
        			var o = ajaxJson(url, data);
        			if(o){
        				this.scriptList = o;
        			}
        			loadAllActionList();
        			loadWorkFlowNodeScriptText();
        			loadWorkFlowNodeActionList();
        		}
        	},
        	selectScriptTabName : function(name){
        		this.tabName = name;
        	},
        	addItem : function(){
        		this.scriptList.push({
        			src : ''
                });
        	},
        	removeItem : function(index){
        		this.scriptList.splice(index, 1);
        	},
        	addAction : function(){
        		var len = this.actionList.length;
        		this.actionList.push({
        			actionname : '',
        			ispreoperator : '1',
        			interfaceid : '',
        			actionorder : len + 1,
        			isused : true
                });
        	},
        	removeAction : function(index){
        		this.actionList.splice(index, 1);
        	},
        	saveScript : function(){
        		if(this.scriptTabName=="2"){
        			var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFNodeScriptSave.jsp";
            		var data = {
            			workFlowId : this.workflowId,
            			nodeId : this.workflowNodeId,
            			scripts : JSON.stringify(this.scriptList)
            		};
            		var o = ajaxJson(url,data);
            		if(o && o.flag){
            			loadWorkFlowNodeScriptText();
            			this.$Message.success('保存成功!');
            		}else{
            			this.$Message.error('保存失败!');
            		}
        		}else if(this.scriptTabName=="3"){
        			var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFNodeActionSave.jsp";
            		var data = {
            			workFlowId : this.workflowId,
            			nodeId : this.workflowNodeId,
            			actionList : JSON.stringify(this.actionList)
            		};
            		var o = ajaxJson(url,data);
            		if(o && o.flag){
            			loadWorkFlowNodeActionList();
            			this.$Message.success('保存成功!');
            		}else{
            			this.$Message.error('保存失败!');
            		}
        		}else{
        			this.$Message.warning('暂无功能!');
        		}
        	}
        }
    });
});

function loadAllActionList(){
	var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxAllActionSetting.jsp";
	var data = {};
	var o = ajaxJson(url,data);
	if(o){
		app.actionAllList = o;
	}
}

function loadWorkFlowInfo(obj){
	if(obj){
		var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWorkFlowInfo.jsp";
		var data = {
			filterValue : obj.filterVa1
		};
		var o = ajaxJson(url,data);
		if(o){
			obj.workflowData = o;
		}
	}
}

function loadWorkFlowFieldInfo(){
	var workflowId = app.workflowId;
	if(workflowId!=""){
		var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFTableInfo.jsp";
		var data = {
			workFlowId : workflowId
		};
		var o = ajaxJson(url, data);
		if(o){
			app.workflowFieldData = o;
			app.showTabs = true;
			loadWorkFlowFieldInfoJSON();
		}else{
			app.workflowFieldData = [];
			app.showTabs = false;
		}
	}else{
		app.workflowFieldData = [];
		app.showTabs = false;
	}
	loadWorkFlowNodeInfo();
}

function loadWorkFlowNodeInfo(){
	var workflowId = app.workflowId;
	if(workflowId!=""){
		var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFNodeInfo.jsp";
		var data = {
			workFlowId : workflowId
		};
		var o = ajaxJson(url, data);
		if(o){
			app.workflowNodeId = '';
			app.workflowNodeData = o;
		}else{
			app.workflowNodeId = '';
			app.workflowNodeData = [];
		}
	}else{
		app.workflowNodeId = '';
		app.workflowNodeData = [];
	}
}

function loadWorkFlowFieldInfoJSON(){
	var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFTableInfoJson.jsp";
	var data = {
		workFlowId : app.workflowId
	};
	var o = ajaxJson(url, data);
	if(o){
		app.fieldJSONText = jsonStringify(o,4);
	}
}

function loadWorkFlowNodeScriptText(){
	var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFNodeScriptText.jsp";
	var data = {
		workFlowId : app.workflowId,
		nodeId : app.workflowNodeId
	};
	var o = ajaxText(url, data);
	if(o){
		app.scriptText = myTrim(o);
		app.scriptTabName = "1";
	}
}

function loadWorkFlowNodeActionList(){
	var url = "/mobile/plugin/westvalley/tools/workflow/jsp/ajaxWFNodeActionInfo.jsp";
	var data = {
		workFlowId : app.workflowId,
		nodeId : app.workflowNodeId
	};
	var o = ajaxJson(url, data);
	if(o){
		app.actionList = o;
	}
}

//格式方法定义
function jsonStringify(data,space){
    var seen=[];
    return JSON.stringify(data,function(key,val){
        if(!val||typeof val !=='object'){
            return val;
        }
        if(seen.indexOf(val)!==-1){
            return '[Circular]';
        }
        seen.push(val);
        return val;
    },space);
}

function myTrim(x) {
    return x.replace(/(^\s*)|(\s*$)/g, ''); 
}

/**
 * 异步提交
 * @param url
 * @param data
 * @returns json
 */
function ajaxJson(url, data) {
	var datas = "";
	jQuery.ajax({
		type : "POST",
		url : url,
		data : data,
		dataType : "json",
		async : false,
		success : function(data) {
			datas = data;
		},
		error: function(x,msg){
			alert(msg);
		}
	});
	return datas;
}

/**
 * 异步提交
 * @param url
 * @param data
 * @returns json
 */
function ajaxText(url, data) {
	var datas = "";
	jQuery.ajax({
		type : "POST",
		url : url,
		data : data,
		dataType : "text",
		async : false,
		success : function(data) {
			datas = data;
		},
		error: function(x,msg){
			alert(msg);
		}
	});
	return datas;
}